/**   
 * @Title: JBeerWeb.java
 * @Package com.jbeer.framework
 * @author Bieber
 * @date 2014-4-22 上午12:08:58
 * @version V1.0   
 */

package com.jbeer.framework;

import java.io.File;
import java.io.IOException;
import java.util.Set;

import org.apache.log4j.Logger;
import org.apache.velocity.app.Velocity;

import com.jbeer.framework.annotation.Controller;
import com.jbeer.framework.constant.JBeerConstant;
import com.jbeer.framework.constant.JBeerConstant.VIEWER;
import com.jbeer.framework.enumeration.BeanScope;
import com.jbeer.framework.exception.GenerateActionException;
import com.jbeer.framework.exception.InitializationException;
import com.jbeer.framework.exception.ScanClassException;
import com.jbeer.framework.ioc.JBeerIOCContainerContext;
import com.jbeer.framework.utils.ClassUtils;
import com.jbeer.framework.utils.LoggerUtil;
import com.jbeer.framework.utils.StringUtils;
import com.jbeer.framework.web.JBeerWebContext;

import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;

/**
 * <p>
 * 类功能说明:MVC 模块
 * </p>
 * <p>
 * 类修改者 修改日期
 * </p>
 * <p>
 * 修改说明
 * </p>
 * <p>
 * Title: JBeerWeb.java
 * </p>
 * 
 * @author Bieber <a
 *         mailto="bieber.jbeer@hotmail.com">bieber.jbeer@hotmail.com</a>
 * @date 2014-4-22 上午12:08:58
 * @version V1.0
 */

public class JBeerWeb {
    private static final Logger logger             = LoggerUtil.generateLogger(JBeerWeb.class);

    public static  String        webTempFileDir     = JBeerConstant.DEFAULT_TEMP_FILE_DIR;

    public static VIEWER        controllerViewer   = VIEWER.JSTL;

    public static String        viewPrefix;

    public static String        viewSuffix;
    /**
     * jstl,freemarker,velocity and so on
     */
    public static String        viewRender         = VIEWER.JSTL.name();

    public static Configuration freemarkerConfig   = null;

    public static String        velocityProperties = null;

    /**
     * 
    * <p>函数功能说明:初始化Web模块，从当前classpath中扫描被注解了{@link Controller}注解的类，并将其注册到IOC容器中</p>
    * <p>Bieber  2014年6月2日</p>
    * <p>修改者名字 修改日期</p>
    * <p>修改内容</a>  
    * @return void
     */
    protected static void init() throws GenerateActionException, ScanClassException,
                                InitializationException {
        if (logger.isDebugEnabled()) {
            logger.debug("initializing web module");
        }
        if (!JBeerWeb.viewPrefix.startsWith("/")) {
            JBeerWeb.viewPrefix = "/" + JBeerWeb.viewPrefix;
        }
        if (!JBeerWeb.viewPrefix.endsWith("/")) {
            JBeerWeb.viewPrefix = JBeerWeb.viewPrefix + "/";
        }
        if (viewRender.equalsIgnoreCase(VIEWER.FREEMARKER.toString())) {
            freemarkerConfig = new Configuration();
            try {
                freemarkerConfig.setDirectoryForTemplateLoading(new File(viewPrefix));
            } catch (IOException e) {
                throw new GenerateActionException(e);
            }
            freemarkerConfig.setObjectWrapper(new DefaultObjectWrapper());
        } else if (viewRender.equalsIgnoreCase(VIEWER.VELOCITY.toString())) {
            if (StringUtils.isEmpty(velocityProperties)) {
                Velocity.init();
            } else {
                Velocity.init(velocityProperties);
            }
        }
        JBeerWebContext.initContext();
        Set<Class<?>> classSet = ClassUtils.scanClassesByAnnotation(Controller.class);
        if (logger.isDebugEnabled()) {
            logger.debug("has found " + classSet.size() + " controller in current application.");
        }
        for (Class<?> clazz : classSet) {
            JBeerIOCContainerContext.registBeanDefinition(clazz,
                JBeerConstant.IOC_DEFALUT_FACTORY_METHOD_NAME, BeanScope.PROTOTYPE,
                JBeerConstant.DEFAULT_BEANNAME);
        }
    }
}
